# -*- coding:utf-8 -*-
# File name : rule_based_agent.py
# Create date : 2022/10/26
# All rights reserved:北京华戍防务技术有限公司
# Author:卡巴司机
# Version: 1.0.0

class CAgent:
    def __init__(self, side):
        self.side = side
        self.scoringlogs = []

        # 获取任务对象
        self.mssn_j20 = side.get_missions_by_name('J20')
        self.mssn_j16 = side.get_missions_by_name('J16')
        self.mssn_su30 = side.get_missions_by_name('SU30')

    def mssn_area_control(self):
        j20_area_control = False
        for contact in self.side.contacts.values():
            if contact.m_ContactType == 0:
                j20_area_control = True
        if j20_area_control:
            target_AC = [v for v in self.side.contacts.values() if v.m_ContactType == 0]
            diag_lon_max = target_AC[0].dLongitude
            diag_lat_max = target_AC[0].dLatitude
            diag_lon_min = target_AC[0].dLongitude
            diag_lat_min = target_AC[0].dLatitude
            for contact in target_AC:
                if contact.dLongitude >= diag_lon_max:
                    diag_lon_max = contact.dLongitude
                if contact.dLatitude >= diag_lat_max:
                    diag_lat_max = contact.dLatitude
                if contact.dLongitude <= diag_lon_min:
                    diag_lon_min = contact.dLongitude
                if contact.dLatitude <= diag_lat_min:
                    diag_lat_min = contact.dLatitude
            self.side.set_reference_point('RP-25', diag_lat_max + 0.2, diag_lon_min - 0.2)
            self.side.set_reference_point('RP-26', diag_lat_max + 0.2, diag_lon_max + 0.2)
            self.side.set_reference_point('RP-27', diag_lat_min - 0.2, diag_lon_max + 0.2)
            self.side.set_reference_point('RP-28', diag_lat_min - 0.2, diag_lon_min - 0.2)

        f14_shootdown_count = 0
        for log in self.scoringlogs:
            if '摧毁' in log and 'F-14E' in log:
                f14_shootdown_count += 1
        if f14_shootdown_count == 2:
            for contact in self.side.contacts.values():
                if contact.m_ContactType == 8:
                    sam = contact
            self.side.set_reference_point('RP-29', 26.82 + 0.2, 126.52 - 0.2)
            self.side.set_reference_point('RP-30', 26.82 + 0.2, 126.52 + 0.2)
            self.side.set_reference_point('RP-31', 26.82 - 0.2, 126.52 + 0.2)
            self.side.set_reference_point('RP-32', 26.82 - 0.2, 126.52 - 0.2)
            self.side.set_reference_point('RP-33', sam.dLatitude + 0.1, sam.dLongitude - 0.1)
            self.side.set_reference_point('RP-34', sam.dLatitude + 0.1, sam.dLongitude + 0.1)
            self.side.set_reference_point('RP-35', sam.dLatitude - 0.1, sam.dLongitude + 0.1)
            self.side.set_reference_point('RP-36', sam.dLatitude - 0.1, sam.dLongitude - 0.1)

        su30_area_control = False
        for log in self.scoringlogs:
            if '损伤' in log and '爱国者' in log:
                su30_area_control = True
        if su30_area_control:
            for contact in self.side.contacts.values():
                if contact.m_ContactType == 2:
                    ship = contact
            self.side.set_reference_point('RP-37', ship.dLatitude + 0.1, ship.dLongitude - 0.1)
            self.side.set_reference_point('RP-38', ship.dLatitude + 0.1, ship.dLongitude + 0.1)
            self.side.set_reference_point('RP-39', ship.dLatitude - 0.1, ship.dLongitude + 0.1)
            self.side.set_reference_point('RP-40', ship.dLatitude - 0.1, ship.dLongitude - 0.1)

    def step(self, side):
        self.side = side
        self.scoringlogs = self.side.m_ScoringLogs.split('@')
        self.mssn_area_control()

